﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jumpton.Data;
using System.Data.SqlClient;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;


namespace Jumpton.SFA.Server.BusinessRule.System
{
    public class RegionManager : BusinessLogic
    {
    
        /// <summary>
        /// Get Region list filled DataTable
        /// </summary>
        /// <param name="codeOrName">Region Code or Name</param>
        /// <param name="orgId">One or More OrgId</param>
        /// <param name="level">Region Level</param>
        /// <param name="valid">Valid:1 or Invalid:0 or All:NULL</param>
        /// <returns></returns>
        public DataTable GetRegionTable(string codeOrName,string orgId, int level, string valid, decimal domainId)
        {
            string sql = string.Format("select * from REGIONS where DOMAIN_ID={0}", domainId);
          
            valid = valid.Trim().Replace("'", "''");
            if (!"".Equals(valid))
            {
                sql += string.Format(" and ACTIVE='{0}'", valid);
            }            

            return SqlData.OpenSql(sql).Tables[0];
        }
        public DataTable GetRegionTable(string codeOrName, int orgId, int level, string valid, decimal domainId)
        {
            string sql = string.Format("select * from REGIONS where DOMAIN_ID={0}", domainId);




            valid = valid.Trim().Replace("'", "''");
            if (!"".Equals(valid))
            {
                sql += string.Format(" and ACTIVE='{0}'", valid);
            }

            return SqlData.OpenSql(sql).Tables[0];
        }

        public DataTable GetRegionTable(string strwhere, decimal domainId,string selectCol)
        {
            string sql = string.Format("select {1} from REGIONS where DOMAIN_ID={0} and ACTIVE = 1 ", domainId,selectCol);
            if (!"".Equals(strwhere))
            {
                sql += strwhere;
            }
            return SqlData.OpenSql(sql).Tables[0];
        }


        //获取所有区域
        public ObjectTable GetRegionData(decimal DomainID,int level,string Code)
        {
            String FieldName = "";
            if (level == 0)
            {
                FieldName = "1";
                Code = "1";
            }
            if (level == 1)
            {
                FieldName = "COUNTRY_CODE";
            }
            else if (level == 2)
            {
                 FieldName ="STATE_CODE";
            }
            else if (level ==3)
            {
                FieldName = "CITY_CODE";
            }
            else  if (level == 4)
            {
                FieldName = "DISTRICT_CODE";
            }
            return new ObjectTable(typeof(REGIONS), string.Format("{0}='{1}' AND ACTIVE=1", FieldName, Code));
        }

        public DataTable GetRegionDistinctData(decimal Domain_Id,int level, string Code)
        {
            String FieldName = "";
            String SELECT_Filed_Names = "";
            if (level == 0)
            {
                FieldName = "'1'";
                Code= "1";
                SELECT_Filed_Names = "COUNTRY_CODE,COUNTRY_NAME";
            }
            if (level == 1)
            {
                FieldName = "COUNTRY_CODE";
                SELECT_Filed_Names = "STATE_CODE,STATE_NAME";
            }
            else if (level == 2)
            {
                FieldName = "STATE_CODE";
                SELECT_Filed_Names = "CITY_CODE,CITY_NAME";
            }
            else if (level == 3)
            {
                FieldName = "CITY_CODE";
                SELECT_Filed_Names = "DISTRICT_CODE,DISTRICT_NAME";
            }
            else if (level == 4)
            {
                FieldName = "DISTRICT_CODE";
            }
            string sql = string.Format("SELECT DISTINCT {0} FROM REGIONS WHERE {1}='{2}' AND ACTIVE=1 AND DOMAIN_ID={3}", SELECT_Filed_Names, FieldName, Code,Domain_Id);

            return SqlData.OpenSql(sql).Tables[0];
        }

       

        
    }
}
